package com.pactera.madp.cp.controller.sidebar.contact;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.common.log.annotation.SysLog;
import com.pactera.madp.cp.api.bo.ContactRelationBo;
import com.pactera.madp.cp.api.dto.contact.ContactUniqueQrCodeDto;
import com.pactera.madp.cp.api.dto.contact.ContactUniqueQrCodeQueryDto;
import com.pactera.madp.cp.api.vo.contact.ContactRelationVo;
import com.pactera.madp.cp.api.vo.contact.ContactUniqueQrCodeVo;
import com.pactera.madp.cp.api.vo.contact.ContactUniqueQrCodeWebVo;
import com.pactera.madp.cp.common.util.WxApiUtils;
import com.pactera.madp.cp.handler.CorpContextHolder;
import com.pactera.madp.cp.service.ContactRelationService;
import com.pactera.madp.cp.service.ContactUniqueQrCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/cuqc")
@AllArgsConstructor
@Api(value = "cuqc", tags = "一客一码管理")
public class ContactUniqueQrCodeController {

    private final ContactUniqueQrCodeService contactUniqueQrCodeService;
    private final ContactRelationService contactRelationService;

    /**
     * 获取邀请客户列表
     * @param page
     * @param dto
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "邀请数据列表分页查询")
    public R<IPage<ContactRelationVo>> getJoinContacts(Page<ContactRelationBo> page, @Validated ContactUniqueQrCodeQueryDto dto) {
        return contactRelationService.getJoinContacts(page, dto);
    }

    /**
     * 获取客户专属二维码
     * @param dto
     * @return
     */
    @GetMapping("/qr-code")
    @ApiOperation(value = "获取客户专属二维码")
    public R<ContactUniqueQrCodeVo> getUniqueQrCode(@Validated ContactUniqueQrCodeDto dto, @RequestParam(value = "file", required = false) MultipartFile file) {
        return contactUniqueQrCodeService.getUniqueQrCode(dto);
    }

    @PostMapping("/qr-code-post")
    @ApiOperation(value = "获取客户专属二维码")
    public R<ContactUniqueQrCodeVo> getUniqueQrCodePost(@Validated ContactUniqueQrCodeDto dto) {
        return contactUniqueQrCodeService.getUniqueQrCode1(dto);
    }

    @GetMapping("/qr-code-web")
    @ApiOperation(value = "获取客户专属二维码-前端获取（用于内网不能访问企微获取图像情景）")
    public R<ContactUniqueQrCodeWebVo> getUniqueQrCodeWeb(@Validated ContactUniqueQrCodeDto dto) {
        Integer corpId = CorpContextHolder.getCorpId();
        String token = WxApiUtils.getAccessTokenContact(corpId);
        ContactUniqueQrCodeWebVo vo = new ContactUniqueQrCodeWebVo();
        vo.setAccessToken(token);
        contactUniqueQrCodeService.getUniqueQrCodeWeb(dto, vo);
        return R.ok(vo);
    }

    @SysLog("一客一码-文件上传")
    @PostMapping("/upload")
    @ApiOperation(value = "文件上传")
    public R<ContactUniqueQrCodeVo> uploadBlog(@RequestParam("file") MultipartFile file, Integer codeId) {
        return contactUniqueQrCodeService.upload(codeId, file);
    }

}
